<template>
  <view>
    <shop-search @click="toSearchPage"></shop-search>
    <view class="cate-container">
      <!-- 左侧 -->
      <scroll-view scroll-y="true" :style="{height: wh + 'px'}" class="scroll-left">
        <block v-for="(item, i) in cateList" :key="i">
          <view :class="['scroll-left-item', i===active ? 'active' : '' ]" @click="activeChange(i)">{{item.cat_name}}
          </view>
        </block>
      </scroll-view>
      <!-- 右侧 -->
      <scroll-view scroll-y="true" :style="{height: wh + 'px'}" class="scroll-right" :scroll-top="scrollTop">
        <view v-for="(item, i) in cateLv2" :key="i" class="cate-lv2">
          <view class="cate-lv2-title">
            / {{item.cat_name}} /
          </view>
          <!-- 三级分类 -->
          <view class="cate-lv3">
            <view class="cate-lv3-item" v-for="(item2, i2) in item.children" :key="i2"
              @click="toGoodsListPage(item2.cat_id)">
              <img :src="item2.cat_icon" alt="">
              <text>{{item2.cat_name}}</text>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>

  </view>
</template>

<script>
  import
  badgeMix
  from "@/mixins/tabbar-badge.js"
  export default {
    mixins: [badgeMix],
    data() {
      return {
        wh: 0,
        cateList: [],
        active: 0,
        cateLv2: [],
        scrollTop: 0
      }
    },
    methods: {
      // 获取分类数据
      async getCateList() {
        const {
          data: res
        } = await uni.$http.get("/api/public/v1/categories")
        // 请求失败
        if (res.meta.status !== 200) {
          return uni.$showMsg()
        }
        // 请求成功
        this.cateList = res.message
        this.cateLv2 = res.message[0].children
        uni.$showMsg("数据请求成功！")
      },

      // 切换索引
      activeChange(i) {
        this.active = i

        this.cateLv2 = this.cateList[i].children

        this.scrollTop = this.scrollTop === 0 ? -1 : 0
      },

      // 跳转到商品页面
      toGoodsListPage(id) {
        uni.navigateTo({
          url: '/subpkg/goods_list/goods_list?goods_id=' + id,
        });
      },

      // 跳转到搜索页面
      toSearchPage() {
        uni.navigateTo({
          url: '/subpkg/search/search'
        });
      },
    },
    onLoad() {
      const sysInfo = uni.getSystemInfoSync()
      this.wh = sysInfo.windowHeight - 50
      this.getCateList()
    }
  }
</script>

<style lang="scss">
  .cate-container {
    display: flex;
    background-color: #fff;

    .scroll-left {
      width: 120px;

      .scroll-left-item {
        background-color: #f7f7f7;
        line-height: 70px;
        font-size: 12px;
        text-align: center;

        &.active {
          position: relative;
          background-color: #fff;

          &::before {
            content: " ";
            display: block;
            width: 3px;
            height: 30px;
            background-color: #C00000;
            position: absolute;
            top: 50%;
            left: 0;
            transform: translateY(-50%);
          }
        }
      }
    }

    .cate-lv2-title {
      font-size: 12px;
      font-weight: 800;
      text-align: center;
      padding: 15px 0;
    }

    .cate-lv3 {
      display: flex;
      flex-wrap: wrap;

      .cate-lv3-item {
        display: flex;
        width: 33.33%;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        margin-bottom: 10px;

        img {
          width: 60px;
          height: 60px;
        }

        text {
          font-size: 12px
        }
      }
    }
  }
</style>